package org.ruoyi.aibox.service.impl;

import org.ruoyi.common.core.utils.MapstructUtils;
import org.ruoyi.core.page.TableDataInfo;
import org.ruoyi.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.ruoyi.aibox.domain.bo.DocAuditLogBo;
import org.ruoyi.aibox.domain.vo.DocAuditLogVo;
import org.ruoyi.aibox.domain.DocAuditLog;
import org.ruoyi.aibox.mapper.DocAuditLogMapper;
import org.ruoyi.aibox.service.IDocAuditLogService;
import org.ruoyi.common.core.utils.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 文档审核日志Service业务层处理
 *
 * @author xpeter
 * @date 2025-04-29
 */
@RequiredArgsConstructor
@Service
public class DocAuditLogServiceImpl implements IDocAuditLogService {

    private final DocAuditLogMapper baseMapper;

    /**
     * 查询文档审核日志
     */
    @Override
    public DocAuditLogVo queryById(Long logId){
        return baseMapper.selectVoById(logId);
    }

    /**
     * 查询文档审核日志列表
     */
    @Override
    public TableDataInfo<DocAuditLogVo> queryPageList(DocAuditLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DocAuditLog> lqw = buildQueryWrapper(bo);
        Page<DocAuditLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询文档审核日志列表
     */
    @Override
    public List<DocAuditLogVo> queryList(DocAuditLogBo bo) {
        LambdaQueryWrapper<DocAuditLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DocAuditLog> buildQueryWrapper(DocAuditLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DocAuditLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTaskId() != null, DocAuditLog::getTaskId, bo.getTaskId());
        lqw.eq(StringUtils.isNotBlank(bo.getOperationType()), DocAuditLog::getOperationType, bo.getOperationType());
        lqw.eq(StringUtils.isNotBlank(bo.getOperationContent()), DocAuditLog::getOperationContent, bo.getOperationContent());
        lqw.eq(bo.getOperationTime() != null, DocAuditLog::getOperationTime, bo.getOperationTime());
        lqw.eq(StringUtils.isNotBlank(bo.getOperationIp()), DocAuditLog::getOperationIp, bo.getOperationIp());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), DocAuditLog::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增文档审核日志
     */
    @Override
    public Boolean insertByBo(DocAuditLogBo bo) {
        DocAuditLog add = MapstructUtils.convert(bo, DocAuditLog.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setLogId(add.getLogId());
        }
        return flag;
    }

    /**
     * 修改文档审核日志
     */
    @Override
    public Boolean updateByBo(DocAuditLogBo bo) {
        DocAuditLog update = MapstructUtils.convert(bo, DocAuditLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DocAuditLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除文档审核日志
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
